<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 正向预查 
        var str = '1123213',
            reg = /1(?=2)/g
        console.log(str.match(reg));

        // 贪婪模式&非贪婪模式
        // 默认是贪婪模式，能匹配多，就不会匹配少
        var str = 'fafsfs{{132}}afdsf{{1134fd}}',
            reg = /{{.*}}/g;
        console.log(str.match(reg)); //{{132}}afdsf{{1134fd}}
        var reg = /{{.*?}}/g; //非贪婪模式 ？
        console.log(str.match(reg)) //["{{132}}", "{{1134fd}}"]

        var str = 'aaaa',
            reg = /\w??/g; //非贪婪模式
        console.log(str.match(reg)); //["", "", "", "", ""]

        //replace 不具备全局匹配，只能匹配一次
        var str = 'JSplusplus',
            reg = /plus/g
            // plus  被替换的内容
            // + 替换内容
        console.log(str.replace('plus', '+')); //JS+plus
        console.log(str.replace(reg, '+')); //JS++

        // xxyy>yyxx
        var str = 'aabbccdd',
            reg = /(\w)\1(\w)\2/g
            // $2 可以拿到子表达式匹配的值
        console.log(str.replace(reg, '$2$2$1$1'));
        // $单次匹配到的值
        // $1是第一个子表达式的值
        // $2是第二个子表式的值
        console.log(str.replace(reg, function($, $1, $2) {
            console.log($, $1, $2); //aabb a b  //ccdd c d
            return $2 + $2 + $1 + $1
        })); //bbaaddcc
        // JS-plus-plus>JSPlusPlus
        var str = 'JS-plus-plus',
            reg = /-(\w)/g;
        // 反向引用一定是一个子表达式
        console.log(str.replace(reg, function($, $1) {
            console.log($, $1);
            return $1.toUpperCase()
        })); //JSPlusPlus

        //JSPlusPlus>js_plus_plus
        var str = 'jsPlusPlus',
            reg = /([A-Z])/g;
        console.log(str.replace(reg, function($, $1) {
            console.log($, $1);
            return '_' + $1.toLowerCase()
        })); //js_plus_plus

        // xxyyzz >XxYyZz
        var str = 'xxyyzz',
            reg = /(\w)\1(\w)\2(\w)\3/g;
        console.log(str.replace(reg, function($, $1, $2, $3) {
            console.log($, $1, $2, $3);
            return $1.toUpperCase() + $1 + $2.toUpperCase() + $2 + $3.toUpperCase() + $3
        })); //XxYyZz

        // aabbcc>a$b$c$
        var str = 'aabbcc',
            reg = /(\w)\1(\w)\2(\w)\3/g;
        // 如果使用$ 使用$$
        console.log(str.replace(reg, '$1$$$2$$$3$$'));

        //正则语法存在的符号，要进行转义
        var str = 'aa\\bb\\cc',
            reg = /\\/g;
        console.log(str.match(reg));
        // aabbcc>abc
        var str = 'aabbcc',
            reg = /(\w)\1(\w)\2(\w)\3/g;
        console.log(str.replace(reg, '$1$2$3')); //abc
        var str = 'aaabbvvvvcc',
            reg = /(\w)\1*/g;
        console.log(str.replace(reg, '$1')); //abvc
        // 100000000000> 100,000,000,000
        var str = '100000000000',
            reg = /(\d{3}\B)/g;
        console.log(str.replace(reg, '$1,')); //100,000,000,000
        var str = '1000000000000';
        var reg = /(?=(\B)(\d{3})+$)/g;
        console.log(str.replace(reg, '$1,'));

        //双大括号替换值{{}}
        var str = '10000{{name}}00000000';
        var reg = /{{(.*?)}}/g;
        console.log(str.replace(reg, function(node, key) {
            console.log(node, key);
            return {
                name: '李三'
            }[key];
        }));
    </script>
</body>

</html>